﻿using System;
using System.Data;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Web.UI.WebControls;
using Me.Amon.Da.Db;
using Me.Amon.Da.Db.OMS;
using Me.Amon.POS.C;
using Me.Amon.Util;

namespace Me.Amon.POS
{
    public partial class UpdatePrice : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                return;
            }

            var user = WebUtil.CurrentUser(Session);
            if (user.Rank < IUser.LEVEL_01)
            {
                Response.Redirect(IEnv.HOME_PAGE);
                return;
            }

            var dba = new RDBEngine();

            var spu = Request["spu"];
            if (!CharUtil.IsValidateHash(spu))
            {
                return;
            }

            ReadSpu(dba, spu);

            dba.Reset();
            ListPrice(dba, spu, 0);
        }

        #region 事件处理
        protected void BtAppend_Click(object sender, EventArgs e)
        {
            var dba = new RDBEngine();
            SavePrice(dba);

            dba.Reset();
            ListPrice(dba, HfSpu.Value, 0);
        }

        protected void BtUpdate_Click(object sender, EventArgs e)
        {
            var dba = new RDBEngine();
            SavePrice(dba);

            dba.Reset();
            ListPrice(dba, HfSpu.Value, 0);
        }

        protected void BtCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/POS/Spus.aspx?cat=" + HfCat.Value);
        }

        protected void LbEdit_Click(object sender, EventArgs e)
        {
            LinkButton btn = sender as LinkButton;
            if (btn == null)
            {
                return;
            }

            var id = btn.CommandArgument;
            if (!CharUtil.IsValidateHash(id))
            {
                return;
            }

            ReadPrice(new RDBEngine(), id);
        }
        #endregion

        #region 分页
        protected void HlHome_Click(object sender, EventArgs e)
        {
            var dba = new RDBEngine();
            var spu = HfId.Value;
            ListPrice(dba, spu, 0);
        }

        protected void HlPage1_Click(object sender, EventArgs e)
        {
            int max = int.Parse(HlPage1.CommandArgument);
            var dba = new RDBEngine();
            var spu = HfId.Value;
            ListPrice(dba, spu, max);
        }

        protected void HlPage2_Click(object sender, EventArgs e)
        {
            int max = int.Parse(HlPage2.CommandArgument);
            var dba = new RDBEngine();
            var spu = HfId.Value;
            ListPrice(dba, spu, max);
        }

        protected void HlPage3_Click(object sender, EventArgs e)
        {
            int max = int.Parse(HlPage3.CommandArgument);
            var dba = new RDBEngine();
            var spu = HfId.Value;
            ListPrice(dba, spu, max);
        }

        protected void HlLast_Click(object sender, EventArgs e)
        {
            int max = int.Parse(HlLast.CommandArgument);
            var dba = new RDBEngine();
            var spu = HfId.Value;
            ListPrice(dba, spu, max);
        }
        #endregion

        public void ListPrice(RDBEngine dba, string spu, int page)
        {
            dba.AddTable(DSpuPrice.TABLE);
            dba.AddWhere(DSpuPrice.SPU_ID, spu);
            dba.AddSort(DSpuPrice.CREATE_TIME);

            var table = dba.ExecuteSelect();
            int cnt = table.Rows.Count;
            //if (cnt < 1)
            //{
            //    RpVips.DataSource = table;
            //    RpVips.DataBind();
            //    return;
            //}

            // 每页行数
            int RPP = 20;
            int num = (cnt - 1) / RPP;
            int p = page - 1;
            if (p + 2 > num)
            {
                p = num - 2;
            }
            if (p < 0)
            {
                p = 0;
            }

            HlHome.Enabled = num > 0 && page > 0;
            LlPrev.Visible = num > 3 && page > 0;

            HlPage1.CommandArgument = p.ToString();
            HlPage1.ForeColor = p == page ? Color.Red : Color.Blue;
            p += 1;
            HlPage1.Text = p.ToString();

            if (num > 0)
            {
                HlPage2.Visible = true;
                HlPage2.CommandArgument = p.ToString();
                HlPage2.ForeColor = p == page ? Color.Red : Color.Blue;
                p += 1;
                HlPage2.Text = p.ToString();
            }
            else
            {
                HlPage2.Visible = false;
            }

            if (num > 1)
            {
                HlPage3.Visible = true;
                HlPage3.CommandArgument = p.ToString();
                HlPage3.ForeColor = p == page ? Color.Red : Color.Blue;
                p += 1;
                HlPage3.Text = p.ToString();
            }
            else
            {
                HlPage3.Visible = false;
            }

            LlNext.Visible = num > 3 && page < num - 3;
            HlLast.Enabled = page < num;
            HlLast.CommandArgument = num.ToString();
            LlPage.Text = string.Format("第 {0}/{1} 页", page + 1, num + 1);

            PagedDataSource pds = new PagedDataSource();
            pds.DataSource = table.DefaultView;
            pds.AllowPaging = true;//允许分页
            pds.PageSize = RPP;//单页显示项数
            pds.CurrentPageIndex = page;

            RpPoints.DataSource = pds;
            RpPoints.DataBind();
        }

        private void ReadSpu(RDBEngine dba, string spu)
        {
            dba.AddTable(DSpu.TABLE);
            dba.AddWhere(DSpu.ID, spu);
            var dt = dba.ExecuteSelect();
            if (dt.Rows.Count != 1)
            {
                return;
            }

            DataRow row = dt.Rows[0];
            HfSpu.Value = spu;
            HfCat.Value = "" + row[DSpu.CAT_ID];
            LlCode.Text = "" + row[DSpu.CODE];
            LlName.Text = "" + row[DSpu.NAME];
            LlUom.Text = "" + row[DSpu.UOM_ID];
            LlSpec.Text = "" + row[DSpu.SPEC];
            LlMode.Text = "" + row[DSpu.MODE];
            LlUsableInv.Text = "" + row[DSpu.USABLE_INV];
        }

        private void ReadPrice(RDBEngine dba, string id)
        {
            dba.AddTable(DSpuPrice.TABLE);
            dba.AddWhere(DSpuPrice.ID, id);
            var dt = dba.ExecuteSelect();
            if (dt.Rows.Count != 1)
            {
                return;
            }

            DataRow row = dt.Rows[0];
            HfId.Value = id;
            TbSpuPrice.Text = CharUtil.FormatPrice((int)row[DSpuPrice.PRICE]);
            TbVipPrice.Text = CharUtil.FormatPrice((int)row[DSpuPrice.VIP_PRICE]);
            CbVipType.SelectedValue = "" + row[DSpuPrice.VIP_TYPE];
            TbProPrice.Text = CharUtil.FormatPrice((int)row[DSpuPrice.PRO_PRICE]);
            CbProType.SelectedValue = "" + row[DSpuPrice.PRO_TYPE];
            TbProName.Text = "" + row[DSpuPrice.PRO_NAME];
            TbDates.Text = "" + row[DSpuPrice.VALID_DATES];
            TbDatet.Text = "" + row[DSpuPrice.VALID_DATET];
            CkStatus.Checked = (CSpuPrice.STATUS_DISABLED == (int)row[DSpuPrice.STATUS]);
        }

        private void SavePrice(RDBEngine dba)
        {
            LlErr.Text = "";
            var id = HfId.Value;

            var spu = HfSpu.Value;
            if (!CharUtil.IsValidateHash(spu))
            {
                return;
            }

            var skuPrice = TbSpuPrice.Text.Trim();
            if (string.IsNullOrWhiteSpace(skuPrice))
            {
                LlErr.Text = "请输入 零售价 ！";
                return;
            }
            var iSkuPrice = CharUtil.ParsePrice(skuPrice, -1);
            if (iSkuPrice < 0)
            {
                LlErr.Text = "零售价 输入格式错误！";
                return;
            }

            var vipPrice = TbVipPrice.Text;
            if (string.IsNullOrWhiteSpace(vipPrice))
            {
                LlErr.Text = "请输入 会员价 ！";
                return;
            }

            var vipType = int.Parse(CbVipType.SelectedValue);
            var iVipPrice = CharUtil.ParsePrice(vipPrice, -1);
            if (iVipPrice < 0)
            {
                LlErr.Text = "会员价 输入格式错误！";
                return;
            }
            if (vipType == CSpuPrice.PRICE_TYPE_RATIO && iVipPrice > 100)
            {
                LlErr.Text = "会员价 应为不大于1的折扣！";
                return;
            }

            var proPrice = TbProPrice.Text;
            if (string.IsNullOrWhiteSpace(proPrice))
            {
                LlErr.Text = "请输入 折扣价 ！";
                return;
            }
            var proType = int.Parse(CbProType.SelectedValue);
            var iProPrice = CharUtil.ParsePrice(proPrice, -1);
            if (iProPrice < 0)
            {
                LlErr.Text = "折扣价 输入格式错误！";
                return;
            }
            if (proType == CSpuPrice.PRICE_TYPE_RATIO && iProPrice > 100)
            {
                LlErr.Text = "折扣价 应为不大于1的折扣！";
                return;
            }

            var proName = TbProName.Text;

            var dates = TbDates.Text.Trim();
            if (string.IsNullOrEmpty(dates))
            {
                LlErr.Text = "请输入折扣 起始日期！";
                return;
            }
            if (!CharUtil.IsValidateDate(dates, true))
            {
                LlErr.Text = "起始日期格式应为 2000/01/01 ！";
                return;
            }
            var datet = TbDatet.Text.Trim();
            if (!string.IsNullOrEmpty(datet) && !CharUtil.IsValidateDate(datet, true))
            {
                LlErr.Text = "结束日期格式应为 2000/01/01 ！";
                return;
            }

            var user = WebUtil.CurrentUser(Session);

            dba.AddTable(DSpuPrice.TABLE);
            dba.AddParam(DSpuPrice.SPU_ID, spu);
            dba.AddParam(DSpuPrice.PRICE, iSkuPrice);
            dba.AddParam(DSpuPrice.VIP_PRICE, iVipPrice);
            dba.AddParam(DSpuPrice.VIP_TYPE, CbVipType.SelectedValue);
            dba.AddParam(DSpuPrice.PRO_PRICE, iProPrice);
            dba.AddParam(DSpuPrice.PRO_TYPE, CbProType.SelectedValue);
            dba.AddParam(DSpuPrice.PRO_NAME, proName);
            dba.AddParam(DSpuPrice.VALID_DATES, dates);
            dba.AddParam(DSpuPrice.VALID_DATET, TbDatet.Text);
            dba.AddParam(DSpuPrice.STATUS, CkStatus.Checked ? CSpuPrice.STATUS_DISABLED : CSpuPrice.STATUE_ENABLED);
            dba.AddParam(DSpuPrice.UPDATE_TIME, DSpuPrice.NOW, false);
            dba.AddParam(DSpuPrice.UPDATE_USER, user.UserId);
            if (CharUtil.IsValidateHash(id))
            {
                dba.AddWhere(DSpuPrice.ID, id);
                dba.ExecuteUpdate();
            }
            else
            {
                id = HashUtil.UtcTimeInHex();
                dba.AddParam(DSpuPrice.ID, id);
                dba.ExecuteInsert();
            }
            ShowDef();
        }

        private void ShowDef()
        {
            TbSpuPrice.Text = "0";
            TbVipPrice.Text = "0";
            TbProPrice.Text = "0";
            TbProName.Text = "";
            TbDates.Text = "";
            TbDatet.Text = "";
        }
    }
}